package com.mycc.game.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mycc.game.damin.Information;
import com.mycc.game.damin.Scenario;
import com.mycc.game.dao.InformationDao;
import com.mycc.game.service.InformationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class InformationServiceImpl implements InformationService {
    @Autowired
    private InformationDao informationDao;

    //查询怪谈所有信息
    @Override
    public IPage<Information> selAllInformation(int pageNum, int pageSize) {
        //创建查询容器
        Page<Information> page = new Page<>(pageNum, pageSize);
        return informationDao.selectPage(page, null);
    }


    //通过Id查询怪谈所有信息
    @Override
    public IPage<Information> selAllInformationById(int id, int pageNum, int pageSize) {
        //创建查询容器
        LambdaQueryWrapper<Information> qew = new LambdaQueryWrapper<>();
        qew.eq(Information::getId, id);
        Page<Information> page = new Page<>(pageNum, pageSize);
        //条件
        return informationDao.selectPage(page, qew);
    }

    //通过名字模糊查询怪谈所有信息
    @Override
    public IPage<Information> selAllInformationByName(String name, int pageNum, int pageSize) {
        LambdaQueryWrapper<Information> qew = new LambdaQueryWrapper<>();
        qew.like(Information::getName, name);
        Page<Information> page = new Page<>(pageNum, pageSize);
        return informationDao.selectPage(page, qew);
    }

    //删除怪谈信息
    @Override
    public boolean delInformation(int id) {
        int x = informationDao.deleteById(id);
        if (x > 0) {
            return true;
        } else {
            return false;
        }
    }

    //新值怪谈信息
    @Override
    public boolean addInformation(Information information) {
        int x = informationDao.insert(information);
        if (x > 0) {
            return true;
        } else {
            return false;
        }
    }

    //修改怪谈信息
    @Override
    public boolean updateInformation(int id, Information information) {
        Information informations = informationDao.selectById(id);
        if (informations != null) {
            BeanUtils.copyProperties(information, informations);
            int x = informationDao.updateById(informations);
            return x > 0;
        }
        return false;
    }
}
